package me.bcloud.web.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import me.bcloud.web.domain.JobConfig;

@Component
public class JobConfigDaoMysqlJdbcImpl implements JobConfigDao {

	@Resource
	private JdbcTemplate jdbcTemplate;

	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}

	@Override
	public List<JobConfig> findAll() {
		String sql = "select * from jobconfigs";
		System.out.println(sql);
		return jdbcTemplate.query(sql, new RowMapper<JobConfig>() {

			@Override
			public JobConfig mapRow(ResultSet rs, int index)
					throws SQLException {
				JobConfig jobConfig = new JobConfig();
				jobConfig.setId(rs.getInt("id"));
				jobConfig.setGname(rs.getString("gname"));
				jobConfig.setPname(rs.getString("pname"));
				jobConfig.setStartid(rs.getInt("startid"));
				jobConfig.setTotalNum(rs.getInt("totalNum"));
				jobConfig.setCount(rs.getInt("count"));
				jobConfig.setCacheNum(rs.getInt("cacheNum"));
				jobConfig.setRunNum(rs.getInt("runNum"));
				jobConfig.setOpen(rs.getInt("open") == 1 ? true : false);
				jobConfig.setDatas(rs.getString("datas"));
				jobConfig.setReadTable(rs.getString("readTable"));
				jobConfig.setWriteTable(rs.getString("writeTable"));
				jobConfig.setTestUrl(rs.getString("testurl"));
				jobConfig.setTestStr(rs.getString("teststr"));
				return jobConfig;
			}

		});

	}

	@Override
	public void update(JobConfig jobConfig) {
		String sql = "update jobconfigs set startid=?, totalNum=?,count=?,cacheNum=?,runNum=?,open=?,datas=?,readTable=?,writeTable=?,testurl=?,teststr=? where gname=?";

		System.out.println(sql);
		jdbcTemplate.update(sql,
				new Object[] { jobConfig.getStartid(), jobConfig.getTotalNum(),
						jobConfig.getCount(), jobConfig.getCacheNum(),
						jobConfig.getRunNum(), jobConfig.isOpen() ? 1 : 0,
						jobConfig.getDatas(), jobConfig.getReadTable(),
						jobConfig.getWriteTable(), jobConfig.getTestUrl(),
						jobConfig.getTestStr(), jobConfig.getGname() });

	}

	@Override
	public void delete(int id) {

		jdbcTemplate.update("delete from jobconfigs where id=?",
				new Object[] { id });
	}

	/*
	 * @Override public JobConfig findByName(String jobname) { String sql =
	 * "select * from jobconfigs where gname=?"; jdbcTemplate.query(sql, new
	 * Object[]{jobname}, new ResultSetExtractor<JobConfig>() {
	 * 
	 * @Override public JobConfig extractData(ResultSet rs) throws SQLException,
	 * DataAccessException { JobConfig jobConfig = new JobConfig();
	 * jobConfig.setId(rs.getInt("id"));
	 * jobConfig.setGname(rs.getString("gname"));
	 * jobConfig.setStartid(rs.getInt("startid"));
	 * jobConfig.setTotalNum(rs.getInt("totalNum"));
	 * jobConfig.setCount(rs.getInt("count"));
	 * jobConfig.setCacheNum(rs.getInt("cacheNum"));
	 * jobConfig.setRunNum(rs.getInt("runNum"));
	 * jobConfig.setOpen(rs.getInt("open") == 1 ? true : false);
	 * jobConfig.setDatas(rs.getString("datas"));
	 * jobConfig.setReadTable(rs.getString("readTable"));
	 * jobConfig.setWriteTable(rs.getString("writeTable")); return jobConfig; }
	 * }); return null; }
	 */
	public JobConfig findById(Integer id) {
		String sql = "select * from jobconfigs where id=?";
		jdbcTemplate.query(sql, new Object[] { id },
				new ResultSetExtractor<JobConfig>() {

					@Override
					public JobConfig extractData(ResultSet rs)
							throws SQLException, DataAccessException {
						JobConfig jobConfig = new JobConfig();
						jobConfig.setId(rs.getInt("id"));
						jobConfig.setGname(rs.getString("gname"));
						jobConfig.setPname(rs.getString("pname"));
						jobConfig.setStartid(rs.getInt("startid"));
						jobConfig.setTotalNum(rs.getInt("totalNum"));
						jobConfig.setCount(rs.getInt("count"));
						jobConfig.setCacheNum(rs.getInt("cacheNum"));
						jobConfig.setRunNum(rs.getInt("runNum"));
						jobConfig.setOpen(rs.getInt("open") == 1 ? true : false);
						jobConfig.setDatas(rs.getString("datas"));
						jobConfig.setReadTable(rs.getString("readTable"));
						jobConfig.setWriteTable(rs.getString("writeTable"));
						jobConfig.setTestUrl(rs.getString("testurl"));
						jobConfig.setTestStr(rs.getString("teststr"));
						return jobConfig;
					}
				});
		return null;
	}

	@Override
	public void insert(JobConfig jobConfig) {

		jdbcTemplate
				.update("insert into jobconfigs (gname,pname,startid,totalNum,count,cacheNum,runNum,datas,readTable,writeTable,testurl,teststr) values(?,?,?,?,?,?,?,?,?,?,?,?)",
						new Object[] { jobConfig.getGname(),
								jobConfig.getPname(), jobConfig.getStartid(),
								jobConfig.getTotalNum(), jobConfig.getCount(),
								jobConfig.getCacheNum(), jobConfig.getRunNum(),
								jobConfig.getDatas(), jobConfig.getReadTable(),
								jobConfig.getWriteTable(),
								jobConfig.getTestUrl(), 
								jobConfig.getTestStr() });

	}

}
